const HOST_URL = "http://127.0.0.1/haijiao/"
const TOKEN_KEY = "jwtToken";

/**
 * 将服务器返回的token存储在本地
 * @param token
 */
function setJwtToken(token) {
    localStorage.setItem(TOKEN_KEY, token);
}

/**
 * 获取本地的token
 * @returns token
 */
function getJwtToken() {
    return localStorage.getItem(TOKEN_KEY);
}

/**
 * 移除token
 */
function removeJwtToken() {
    localStorage.removeItem(TOKEN_KEY);
}

/**
 * 创建带Token  Header的请求头、
 * @returns
 */
function createAuthorizationTokenHeader() {
    token = getJwtToken();
    if (token) {
        return {'Authorization': token}
    } else {
        return {};
    }
}

function form_con() {


    var name = document.getElementById("L_label").value;  // 提取邮箱或手机号
    // 发起登录请求
    var params = {
        "name": name,

    };
    $.ajax({
        url: "/labels/",
        type: "post",
        // data: params,
        data: JSON.stringify(params),
        contentType: "application/json",
        success: function (resp) {
            if (resp.code == "0") {
                // 刷新当前界面
                alert(resp.errmsg);
                location.href = "/labels"
            } else {
                alert(resp.errmsg);
            }
        }
    })

}

function Compile() {
    $.ajax({
        url: "/labels/settlement/",
        type: "post",
        // data: params,
        contentType: "application/json",
        success: function (resp) {
            if (resp.code == "0") {
                // 刷新当前界面
                alert(resp.errmsg);
                location.href = "/labels"
            } else {
                alert(resp.errmsg);
            }
        }
    })
}

function form_add() {

    var title = document.getElementById("item").value;
    var A = document.getElementById("A").value;
    var B = document.getElementById("B").value;
    var C = document.getElementById("C").value;
    var D = document.getElementById("D").value;
    var analysis = document.getElementById("analysis").value;
    var list = $('input:radio[name="list"]:checked').val();
    var answer = $('input:radio[name="answer"]:checked').val();
    if (!title) {
        alert("请输入标题");
        return;
    }
    if (!A) {
        alert("请输入A选项的内容");
        return;
    }
    if (!B) {
        alert("请输入B选项的内容");
        return;
    }
    if (!C) {
        alert("请输入C选项的内容");
        return;
    }
    if (!D) {
        alert("请输入D选项的内容");
        return;
    }
    var params = {

        "title": title,
        'A': A,
        'B': B,
        'C': C,
        'D': D,
        'analysis': analysis,
        'list': list,
        'answer': answer

    };
    myajax = $.ajax({
        url: "/add/",
        type: "post",
        // data: params,
        data: JSON.stringify(params),
        contentType: "application/json",
        success: function (resp) {
            if (resp.code == "0") {
                // 刷新当前界面
                alert(resp.errmsg);
                window.top.location.reload();

            } else {
                alert(resp.errmsg);
            }
        }
    });

}

function form_add_essay() {

    var title = document.getElementById("item").value;
    var answer = document.getElementById("answer").value;
    var analysis = document.getElementById("analysis").value;
    var list = $('input:radio[name="list"]:checked').val();

    if (!title) {
        alert("请输入标题");
        return;
    }
    if (!answer) {
        alert("请输入答案内容");
        return;
    }

    var params = {

        "title": title,
        'analysis': analysis,
        'list': list,
        'answer': answer

    };
    myajax = $.ajax({
        url: "/add/essay",
        type: "post",
        // data: params,
        data: JSON.stringify(params),
        contentType: "application/json",
        success: function (resp) {
            if (resp.code == "0") {
                // 刷新当前界面
                alert(resp.errmsg);
                window.top.location.reload();

            } else {
                alert(resp.errmsg);
            }
        }
    });

}

function form_add_multiple() {

    var title = document.getElementById("item").value;
    var A = document.getElementById("A").value;
    var B = document.getElementById("B").value;
    var C = document.getElementById("C").value;
    var D = document.getElementById("D").value;
    var E = document.getElementById("E").value;
    var F = document.getElementById("F").value;
    var analysis = document.getElementById("analysis").value;
    var list = $('input:radio[name="list"]:checked').val();
    var ids = [];
    //$('input:checkbox:checked') 等同于 $('input[type=checkbox]:checked')
    //意思是选择被选中的checkbox
    $.each($('input:checkbox:checked'), function () {
        ids.push($(this).val());
    });
    if (!title) {
        alert("请输入标题");
        return;
    }
    if (!A) {
        alert("请输入A选项的内容");
        return;
    }
    if (!B) {
        alert("请输入B选项的内容");
        return;
    }
    if (!C) {
        alert("请输入C选项的内容");
        return;
    }
    if (!D) {
        alert("请输入D选项的内容");
        return;
    }
    if (!E) {
        alert("请输入E选项的内容");
        return;
    }
    if (!F) {
        alert("请输入F选项的内容");
        return;
    }
    var params = {

        "title": title,
        'A': A,
        'B': B,
        'C': C,
        'D': D, 'E': E, 'F': F,
        'analysis': analysis,
        'list': list,
        'answer': ids

    };
    myajax = $.ajax({
        url: "/add/multiple",
        type: "post",
        // data: params,
        data: JSON.stringify(params),
        contentType: "application/json",
        success: function (resp) {
            if (resp.code == "0") {
                // 刷新当前界面
                alert(resp.errmsg);
                window.top.location.reload();

            } else {
                alert(resp.errmsg);
            }
        }
    });

}

function form_add_true() {

    var title = document.getElementById("item").value;
    var analysis = document.getElementById("analysis").value;
    var list = $('input:radio[name="list"]:checked').val();
    var answer = $('input:radio[name="answer"]:checked').val();
    if (!title) {
        alert("请输入标题");
        return;
    }

    var params = {

        "title": title,

        'analysis': analysis,
        'list': list,
        'answer': answer

    };
    myajax = $.ajax({
        url: "/add/true",
        type: "post",
        // data: params,
        data: JSON.stringify(params),
        contentType: "application/json",
        success: function (resp) {
            if (resp.code == "0") {
                // 刷新当前界面
                alert(resp.errmsg);
                window.top.location.reload();

            } else {
                alert(resp.errmsg);
            }
        }
    });

}
